package com.jcfk.eam.service.base;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.eam.domain.po.base.EamMakerContact;
import com.jcfk.eam.domain.query.base.EamMakerContactQuery;
import com.jcfk.eam.domain.dto.base.EamMakerContactDTO;
import com.jcfk.eam.dao.base.EamMakerContactDao;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 制造商联系人
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Service
public class EamMakerContactService extends BaseService<EamMakerContactDao,EamMakerContact> {

    /**
     * EamMakerContact分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMakerContactDTO> queryByPage(DataPage<EamMakerContactDTO> page, EamMakerContactQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamMakerContact是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMakerContactDTO dto) {
        QueryWrapper<EamMakerContact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入制造商联系人
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMakerContactDTO dto) {

        // 保存
        EamMakerContact item = new EamMakerContact();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新制造商联系人
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMakerContactDTO dto) {

        // 保存
        EamMakerContact item = new EamMakerContact();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除制造商联系人(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMakerContactDTO dto) {

        EamMakerContact item = new EamMakerContact();

        this.setDefaultValue(item);
        item.setDeleted(1);

        QueryWrapper<EamMakerContact> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMakerContact item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setDeleted(0);
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());


    }

    /**
     * 根据主键获取制造商联系人信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMakerContactDTO get(String id) {
        // 查询数据
        EamMakerContact item = this.baseMapper.selectById(id);
        EamMakerContactDTO dto = new EamMakerContactDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 制造商联系人批量保存
     *
     * @param dtos
     * @return StandardCodeDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<EamMakerContactDTO> dtos, String id) {
        List<EamMakerContact> insertList = new ArrayList<>();
        List<EamMakerContact> updateList = new ArrayList<>();
        List<String> notDeleteIds = new ArrayList<>();


        for (EamMakerContactDTO dto : dtos) {
            EamMakerContact item = new EamMakerContact();
            BeanUtils.copyProperties(dto, item);
            this.setDefaultValue(item);

            if (!StringUtils.isEmpty(dto.getId())) {
                notDeleteIds.add(dto.getId());
                updateList.add(item);
            } else {
                insertList.add(item);
            }
        }

        // 删除
        QueryWrapper<EamMakerContact> queryWrapper = new QueryWrapper<>();
        if (notDeleteIds.size() > 0) queryWrapper.notIn("id", notDeleteIds);
        queryWrapper.lambda().eq(EamMakerContact::getMakerId, id);
        this.remove(queryWrapper);

        // 保存新增
        if (insertList.size() > 0) this.saveBatch(insertList);

        // 保存更新
        if (updateList.size() > 0) this.updateBigBatch(updateList);
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMakerContactDTO>
     */
    public List<EamMakerContactDTO> getList(EamMakerContactQuery query) {
        // 查询条件
        QueryWrapper<EamMakerContact> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerId()), EamMakerContact::getMakerId, query.getMakerId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerId()), EamMakerContact::getMakerId, query.getMakerId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getOrganizationId()), EamMakerContact::getOrganizationId, query.getOrganizationId());
        qw.lambda().eq(query.getDeleted() != null, EamMakerContact::getDeleted, query.getDeleted());
        // 查询数据
        List<EamMakerContact> list = super.list(qw);

        return convertList(list, EamMakerContactDTO.class);
    }

    /**
     * 根据主键获取制造商联系人信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamMakerContact item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

